import "@babel/polyfill";
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import store from "./store";
import mixin from "@/mixin/global";
import VueRouter from "vue-router";
//此VueRouter是自己自定义引入暴露出来的，即是自定义的，以下的VueRouter同样是这样
// 解决两次访问相同路由地址报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.mixin(mixin);
Vue.use(ElementUI);
Vue.config.productionTip = false;

store.commit("update",{
  staticApi : process.env.VUE_APP_STATIC_API
});

// let staticPage = ['Static', 'Productlist'];

document.title = "CW大药房";
// 路由守卫
router.beforeEach(async (to, from, next) => {
  try {
    // topMenu切换不相干页时取消显示title
    // if (staticPage.indexOf(to.name) === -1) {
    //   let res = store.state.topMenuChecked;
    //   res.hoverTitleItemed = -1;
    //   store.commit("utopMenuChecked", res);
    // } 
    // console.log(to);

    // 动态更新title
    // document.title = to.query.title || to.meta.title;
    // 变更路由栈
    let list = store.state.routeList;
    if (list.length > 20) list = list.shift();
    if (to.fullPath !== list[list.length -1]) {
      store.commit("routeList",[...store.state.routeList, to.fullPath]);
    }
    // 有token时则不返回登录页
    let loginPages = mixin.data().loginPages;
    if (localStorage.getItem("token") &&  loginPages.includes(to.fullPath)) {
      // 存在token且要去login页
      return next('/home');
    }
    next();
  } catch (e) {
    // document.body.innerHTML = e.message;
  }
});
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
